define(['app'], function(app)
{
    app.directive('accordion', function() {  //父指令
        return {
            restrict : 'EA',
            replace : true,
            transclude : true,
            template : '<div ng-transclude></div>',
            controller : function() {
                var expanders = [];
                this.gotOpened = function(selectedExpander) {
                    angular.forEach(expanders, function(expander) {
                        if (selectedExpander != expander) {
                            expander.showMe = false;
                        }
                    });
                }
                this.addExpander = function(expander) {
                    expanders.push(expander);
                }
            }
        }
    });
    app.directive('expander', function() {
        return {
            restrict : 'EA',
            replace : true,
            transclude : true,
            require : '^?accordion', // ？不要抛出异常，^允许查找父元素accordion的controller
            scope : {
                title : '=expanderTitle'
            },
            template : '<div>'
                       + '<div class="title" ng-click="toggle()">{{title}}</div>'
                       + '<div class="body" ng-show="showMe" ng-transclude></div>'
                       + '</div>',
            link : function(scope, element, attrs, accordionController) {
                scope.showMe = false;
                accordionController.addExpander(scope);
                scope.toggle = function toggle() {
                    scope.showMe = !scope.showMe;
                    accordionController.gotOpened(scope);
                }
            }
        }
    });
});
